﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Modeling_Sub_Pages_NewsPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            if (Session["WelcomeUser"] != null)
            {
                if (Session["EmployeeID"] != null && Session["Role"].ToString().Equals("ADM"))
                {
                    // Ko chay lai doan nay khi postback
                    if (!IsPostBack)
                    {
                        //Lay id truyen tu querry string
                        string id = Request.QueryString["id"].ToString();

                        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

                        // Load thong tin event co id tren vao form
                        DataSet ds = new DataSet();
                        ds = udm.showSubjectByID(udm.createConnection(), id);

                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            string name = ds.Tables[0].Rows[0][1].ToString();
                            string description = ds.Tables[0].Rows[0][2].ToString();
                            string lv = ds.Tables[0].Rows[0][3].ToString();
                            // Bat dau do vao form
                            txtDName.Text = name;
                            txtDescription.Text = description;
                            txtlv.Text = lv;
                        }
                    }
                }
                else
                {
                    Response.Redirect("~/LoginMessage.aspx?id=1");
                }
            }
            else
            {
                Response.Redirect("~/Login.aspx?id=2");
            }
        }
        else
        {
            Response.Redirect("~/ErrorPage.aspx");
        }
    }
    
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtlv.Text = "";
        txtDName.Text = "";
        txtDescription.Text = "";
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        //Lay id truyen tu querry string
        string id = Request.QueryString["id"].ToString();

        string name = txtDName.Text;
        string description = txtDescription.Text;
        int lv = Convert.ToInt32(txtlv.Text);

        Response.Write(description);

        // Vào MaintainMessage.aspx để xem thông báo
        if (!udm.updateSubject(udm.createConnection(), id, name, description, lv))
        {
            Response.Redirect("MaintainMessage.aspx?ID=13");
        }
        else
        {
            Response.Redirect("MaintainMessage.aspx?ID=12");
        }

    }
}